package edu.univercidade.tcc.web;

import java.util.Set;

import org.hibernate.tuple.Property;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.roo.addon.web.mvc.controller.RooConversionService;

import com.google.common.base.Joiner;

import edu.univercidade.tcc.server.model.Issue;
import edu.univercidade.tcc.server.model.IssueStatus;
import edu.univercidade.tcc.server.model.Priority;
import edu.univercidade.tcc.server.model.Project;
import edu.univercidade.tcc.server.model.ProjectMember;
import edu.univercidade.tcc.server.model.ProjectRole;
import edu.univercidade.tcc.server.model.Tracker;
import edu.univercidade.tcc.server.model.Workflow;

/**
 * A central place to register application converters and formatters.
 */
@RooConversionService
public class ApplicationConversionServiceFactoryBean extends
		FormattingConversionServiceFactoryBean {

	Converter<Set<Property>, String> getPropertySetConverter() {
		return new Converter<Set<Property>, String>() {
			public String convert(Set<Property> properties) {
				return Joiner.on(",").join(properties.toArray());
			}
		};
	}

	public void installLabelConverters(FormatterRegistry registry) {
		registry.addConverter(getPropertySetConverter());
		registry.addConverter(getIssueToStringConverter());
		registry.addConverter(getIssueStatusToStringConverter());
		registry.addConverter(getPriorityToStringConverter());
		registry.addConverter(getProjectToStringConverter());
		registry.addConverter(getProjectMemberToStringConverter());
		registry.addConverter(getProjectRoleToStringConverter());
		registry.addConverter(getTrackerToStringConverter());
		registry.addConverter(getWorkflowToStringConverter());
	}

	public Converter<Issue, String> getIssueToStringConverter() {
		return new org.springframework.core.convert.converter.Converter<edu.univercidade.tcc.server.model.Issue, java.lang.String>() {
			public String convert(Issue issue) {
				return Joiner.on(" ").join(issue.getId(), issue.getSubject());
			}
		};
	}

	public Converter<Project, String> getProjectToStringConverter() {
		return new org.springframework.core.convert.converter.Converter<edu.univercidade.tcc.server.model.Project, java.lang.String>() {
			public String convert(Project project) {
				return project.getName();
			}
		};
	}

	public Converter<IssueStatus, String> getIssueStatusToStringConverter() {
		return new org.springframework.core.convert.converter.Converter<edu.univercidade.tcc.server.model.IssueStatus, java.lang.String>() {
			public String convert(IssueStatus issueStatus) {
				return issueStatus.getName();
			}
		};
	}

	public Converter<Priority, String> getPriorityToStringConverter() {
		return new org.springframework.core.convert.converter.Converter<edu.univercidade.tcc.server.model.Priority, java.lang.String>() {
			public String convert(Priority priority) {
				return priority.getName();
			}
		};
	}

	public Converter<ProjectMember, String> getProjectMemberToStringConverter() {
		return new org.springframework.core.convert.converter.Converter<edu.univercidade.tcc.server.model.ProjectMember, java.lang.String>() {
			public String convert(ProjectMember projectMember) {
				return Joiner.on(" ").join(projectMember.getFirstname(),
						projectMember.getLastname());
			}
		};
	}

	public Converter<ProjectRole, String> getProjectRoleToStringConverter() {
		return new org.springframework.core.convert.converter.Converter<edu.univercidade.tcc.server.model.ProjectRole, java.lang.String>() {
			public String convert(ProjectRole projectRole) {
				return projectRole.getName();
			}
		};
	}

	public Converter<Tracker, String> getTrackerToStringConverter() {
		return new org.springframework.core.convert.converter.Converter<edu.univercidade.tcc.server.model.Tracker, java.lang.String>() {
			public String convert(Tracker tracker) {
				return tracker.getName();
			}
		};
	}

	public Converter<Workflow, String> getWorkflowToStringConverter() {
		return new org.springframework.core.convert.converter.Converter<edu.univercidade.tcc.server.model.Workflow, java.lang.String>() {
			public String convert(Workflow workflow) {
				return Joiner.on("=>").join(workflow.getOldIssueStatus(),
						workflow.getNewIssueStatus());
			}
		};
	}

}
